﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.BLL;
using Register.Model;
using System.Web.Script.Serialization;
using RegisterWeb.Manager.RegistrationFeeManager.ashx;
using System.Text;

namespace RegisterWeb.Manager.HospitalManager.ashx
{
    /// <summary>
    /// DepartmentsManagerService 的摘要说明
    /// </summary>
    public class DepartmentsManagerService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            String action = context.Request["action"];

            //获取科室的全部列表
            if (action.Equals("list"))
            {
                DepartmentListJSON departmentListJSON = new DepartmentListJSON();
            

                departmentListJSON.rows = new List<DepartmentJSON>();

                 int row = int.Parse(context.Request["rows"].ToString());  
                int page = int.Parse(context.Request["page"].ToString());

                List<DepartmentsInfo> list = DepartmentsBLL.GetPagedDepartmentsInfo(row, page, " Departments_State='1' ");
                departmentListJSON.total = DepartmentsBLL.GetDepartmentsCount("  Departments_State='1' ");

                foreach (DepartmentsInfo de in list)
                {
                    string status;
                    if (de.Departments_State.ToString().Equals("1"))
                    {
                        status = "有效";
                    }
                    else
                        status = "无效";
                    departmentListJSON.rows.Add(new DepartmentJSON(de.Departments_ID, (HospitalInfoBLL.GetHospitalInfoByID(de.Hospital_ID)).Hospital_Name, de.Departments_Name, de.Departments_Introduce, de.Departments_AddTime.ToString(), de.Departments_Recoder, status,de.Hospital_ID));
                }

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(departmentListJSON));
            }
          //添加科室
            else if (action.Equals("add"))
            {
                String DepartmentName = context.Request["textDepartmentName"];
                String DepartmentDes = context.Request["textDepartmentDes"];
                String selectHosptial = context.Request["selectHosptial"];
                DepartmentsInfo info = new DepartmentsInfo();
                info.Departments_ID = DataIDHelper.GetDataID("Departments_ID");
                info.Departments_Introduce = DepartmentDes;
                info.Departments_Name = DepartmentName;
                info.Departments_LastAmendPerson = "admin";
                info.Departments_Recoder = "admin";
                info.Departments_LastAmendTime = DateTime.Now;
                info.Departments_AddTime = DateTime.Now;
                info.Hospital_ID = selectHosptial;
                info.Departments_State="1";

                if (DepartmentsBLL.AddDepartments(info))
                {
                    message msg = new message(true,"该科室添加成功！");
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    context.Response.Write(jss.Serialize(msg));
                }
                else
                {
                    message msg = new message(false, "该科室添加失败！");
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    context.Response.Write(jss.Serialize(msg));
                }

            }
                //删除科室
            else if (action.Equals("delete"))
            {
                String id = context.Request["id"];
                  DepartmentsInfo info = new DepartmentsInfo();
                info.Departments_ID=id;
                if (DepartmentsBLL.DeleteDepartments(info))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }
                //编辑科室
            else if (action.Equals("edit"))
            {
                String DepartmentName = context.Request["textDepartmentName"];
                String DepartmentDes = context.Request["textDepartmentDes"];
                String selectHosptial = context.Request["selectHosptial"];
                String id = context.Request["id"];
                DepartmentsInfo info = new DepartmentsInfo();
                info.Departments_ID = id;
                info.Departments_Name = DepartmentName;
                info.Departments_Introduce = DepartmentDes;
                info.Hospital_ID = selectHosptial;
                info.Departments_LastAmendPerson = "admin";
                info.Departments_LastAmendTime = DateTime.Now;
                if (DepartmentsBLL.UpdateDepartments(info))
                {
                    message msg = new message(true, "该科室更新成功！");
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    context.Response.Write(jss.Serialize(msg));
                }
                else
                {
                    message msg = new message(false, "该科室更新失败！");
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    context.Response.Write(jss.Serialize(msg));
                }
            }
                //查询科室
            else if (action.Equals("search"))
            {
                string hospName = context.Request["hospName"];
                string depName = context.Request["depName"];
                DepartmentListJSON departmentListJSON = new DepartmentListJSON();
                departmentListJSON.rows = new List<DepartmentJSON>();

                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());

                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append(" Departments_State='1' ");
                if (!String.IsNullOrEmpty(depName))
                {
                    strBuilder.Append(" and Departments_Name=").Append(depName).Append(" ");
                }
                if (!String.IsNullOrEmpty(hospName))
                {
                    strBuilder.Append(" and Hospital_ID='").Append(hospName).Append("' ");
                }

                List<DepartmentsInfo> list = DepartmentsBLL.GetPagedDepartmentsInfo(row, page, strBuilder.ToString());
                departmentListJSON.total = DepartmentsBLL.GetDepartmentsCount(strBuilder.ToString());

                foreach (DepartmentsInfo de in list)
                {
                    string status;
                    if (de.Departments_State.ToString().Equals("1"))
                    {
                        status = "有效";
                    }
                    else
                        status = "无效";
                    departmentListJSON.rows.Add(new DepartmentJSON(de.Departments_ID, (HospitalInfoBLL.GetHospitalInfoByID(de.Hospital_ID)).Hospital_Name, de.Departments_Name, de.Departments_Introduce, de.Departments_AddTime.ToString(), de.Departments_Recoder, status, de.Hospital_ID));
                }

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(departmentListJSON));
            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    class DepartmentListJSON
    {
        public int total { get; set; }
        public List<DepartmentJSON> rows { get; set; }
    }

    class DepartmentJSON
    {
        public string ID { get; set; }
        public string Hospital { get; set; }
        public string Name { get; set; }
        public string Introduce { get; set; }
        public string AddTime { get; set; }
        public string Recoder { get; set; }
        public string State { get; set; }
        public string HosptialID { get; set; }

        public DepartmentJSON(string ID, string Hospital, string Name, string Introduce, string AddTime, string Recoder, string State, string HospitalID)
        {
            this.ID = ID;
            this.Hospital = Hospital;
            this.Name = Name;
            this.Introduce = Introduce;
            this.AddTime = AddTime;
            this.Recoder = Recoder;
            this.State = State;
            this.HosptialID = HospitalID;
        }

    }

}